package com.mitac.mcd.cs.mobiletracking.directoryserver.util;

import java.io.PrintStream;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BusinessUtil
{
	public static Log sLogger = LogFactory.getLog("system");
	public static Log aLogger = LogFactory.getLog("application"); 
	
    
    
	/**
     * 获取传递日期所在周的周一
     * @param date
     */
    public static void getFirstDayOfWeek(Calendar date){
    	int day_of_week = date.get(Calendar.DAY_OF_WEEK);
    	if(day_of_week!=1){
    		day_of_week = day_of_week -2;
    	}else{
    		day_of_week = 6;
    	}
    	date.add(Calendar.DATE, -day_of_week);
    }
    
    /**
     * 获取传递日期所在周的周日
     * @param date
     */
    public static void getLastDayOfWeek(Calendar date){
		int day_of_week = date.get(Calendar.DAY_OF_WEEK);
		if(day_of_week!=1){
			day_of_week = date.get(Calendar.DAY_OF_WEEK) - 2;
			date.add(Calendar.DATE, -day_of_week);
			date.add(Calendar.DATE, 6);
		}
    }
    
    /**
     * 获取传递日期所在月的第一天
     * @param date
     */
    public static Date getFirstDayOfMonth(Date date){
    	Calendar calendar = new GregorianCalendar();
    	calendar.setTime(date);
    	calendar.set(Calendar.DATE,calendar.getActualMinimum(Calendar.DATE));
    	return calendar.getTime();
    }
    
    /**
     * 获取传递日期所在月的最后一天
     * @param date
     */
    public static Date getLastDayOfMonth(Date date){
    	Calendar calendar = new GregorianCalendar();
    	calendar.setTime(date);
    	calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DATE));
    	return calendar.getTime();
    }
    
    /**
     * 将制定的参数通过指定的PrintStream对象发生给client端
     * @param cmd
     * @param ip
     * @param port
     * @param status
     * @param errorCode
     * @param PS
     */
    public static void sendMessage(String cmd,String ip,String port,int status,String errorCode,PrintStream PS){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cmd", cmd);
        map.put("ip", ip);
        map.put("port", port);
        map.put("status", status);
        map.put("error", errorCode);
        JSONObject json = JSONObject.fromObject(map);
        BusinessUtil.aLogger.info("开始返回json格式信息到client端..."+json.toString());
        PS.println(json.toString());
        PS.flush();
        BusinessUtil.aLogger.info("完成返回json格式信息到client端..."+json.toString());
    }
    
    /**
     * 将制定的参数通过指定的PrintStream对象发生给client端
     * @param cmd
     * @param status
     * @param errorCode
     * @param PS
     */
    public static void sendMessage(String cmd,int status,String errorCode,PrintStream PS){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cmd", cmd);
        map.put("status", status);
        map.put("error", errorCode);
        JSONObject json = JSONObject.fromObject(map);
        BusinessUtil.aLogger.info("开始返回json格式信息到client端..."+json.toString());
        PS.println(json.toString());
        PS.flush();
        BusinessUtil.aLogger.info("完成返回json格式信息到client端..."+json.toString());
    }
    
    /**
     * 判断字符串中cmd是属于那种类型，并返回该类型的枚举
     * @param s
     * @return
     */
    public static ClientCMDEnum.CMD judge(JSONObject jsonObject){

        ClientCMDEnum.CMD flag = null;
        try {
            if (jsonObject.get("cmd").equals("socketsvrreq")){
                flag = ClientCMDEnum.CMD.socketsvrreq;
            }else{
            	flag = ClientCMDEnum.CMD.notgood;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    
    public static void main(String args[]) throws InterruptedException, InstantiationException, IllegalAccessException{
    	Calendar date = Calendar.getInstance();
    	System.out.println(date.get(Calendar.DAY_OF_MONTH));
    }
}
